﻿using System;
using RpcHelper;
using SqlExecHelper;
using SqlExecHelper.SetColumn;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Service.DAL
{
        internal class AutoTaskPlanDAL : SqlBasicClass
        {
                public AutoTaskPlanDAL () : base ("AutoTaskPlan", "RpcExtendService")
                {

                }

                public Guid Add (AutoTaskPlan plan)
                {
                        plan.Id = Tools.NewGuid ();
                        base.Insert (plan);
                        return plan.Id;
                }
                public AutoTaskPlan[] Gets (Guid taskId)
                {
                        return base.Get<Guid, AutoTaskPlan> ("TaskId", taskId);
                }
                public AutoTaskPlan Get (Guid id)
                {
                        return base.GetRow<Guid, AutoTaskPlan> ("Id", id);
                }

                public void Delete (Guid id)
                {
                        if (!base.Drop ("Id", id))
                        {
                                throw new ErrorException ("rpc.task.plan.drop.fail");
                        }
                }

                internal void Set (Guid id, TaskPlanSet set)
                {
                        if (!base.Update (set, "Id", id))
                        {
                                throw new ErrorException ("rpc.task.plan.set.fail");
                        }
                }
                internal void SetIsEnable (Guid id, bool isEnable)
                {
                        if (!base.Update (new ISqlSetColumn[] {
                                new SqlSetColumn("IsEnable", System.Data.SqlDbType.Bit){ Value=isEnable}
                        }, "Id", id))
                        {
                                throw new ErrorException ("rpc.task.plan.set.fail");
                        }
                }
                internal bool CheckIsRepeat (Guid taskId, string planOnlyNo)
                {
                        return base.CheckIsExists (new ISqlWhere[] {
                                new SqlWhere("TaskId", System.Data.SqlDbType.UniqueIdentifier){Value=taskId},
                                new SqlWhere("PlanOnlyNo", System.Data.SqlDbType.VarChar,32){Value=planOnlyNo}
                        });
                }
        }
}
